package com.jzr.pda.db1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.common.util.U;
import com.jzr.pda.common.SecurityHelper;
import com.jzr.pda.db1.mapper.MyPdaPageMapper;
import com.jzr.pda.db1.mapper.TPdaPageDataMapper;
import com.jzr.pda.db1.model.TPdaPage;
import com.jzr.pda.db1.model.TPdaPageData;
import com.jzr.pda.db1.service.TPdaPageService;
import com.jzr.pda.db1.vo.PdaPageControlVo;
import com.jzr.pda.db1.vo.PdaPageDataVo;
import com.jzr.pda.db1.vo.PdaPageVo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TPdaPageServiceImpl extends ServiceImpl<MyPdaPageMapper, TPdaPage> implements TPdaPageService {

    @Autowired
    private TPdaPageDataMapper pageDataMapper;

    @Override
    public PdaPageVo getPage(String code, Integer dataId) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        PdaPageVo pdaPageVo = baseMapper.getPage(code);
        if(dataId != null && dataId.intValue()>0 && pdaPageVo !=null && pdaPageVo.getControlList() != null){
            TPdaPageData tPdaPageData = pageDataMapper.selectById(dataId);
            for(PdaPageControlVo control : pdaPageVo.getControlList()){
                if(control != null) {
                    String fieldName = control.getFieldName();
                    if (U.isNotBlank(fieldName)) {
                        String val = BeanUtils.getProperty(tPdaPageData, fieldName);
                        BeanUtils.setProperty(control, "defaultValue", val);
                    }
                }
            }
        }
        return pdaPageVo;
    }

    @Override
    public List<PdaPageDataVo> listData(String patientCode, String patientCureNo) {
        return baseMapper.listData(patientCode,patientCureNo);
    }

    @Override
    public TPdaPageData getDataById(Integer id) {
        return pageDataMapper.selectById(id);
    }

    @Override
    public int saveData(TPdaPageData pageData) {
        Integer userId = SecurityHelper.getUserId();
        pageData.setUpdateBy(userId);
        pageData.setUpdateTime(LocalDateTime.now());

        if(pageData == null){
            return 0;
        }
        int count = 0;
        //新增
        if(pageData.getId() == null){
            count = pageDataMapper.insert(pageData);
        }else{ //更新
            count = pageDataMapper.updateById(pageData);
        }

        return count;
    }

    @Override
    public int deleteData(Integer id) {
        Integer userId = SecurityHelper.getUserId();

        if(id == null){
            return 0;
        }
        int count = 0;
        TPdaPageData tPdaPageData = new TPdaPageData();
        tPdaPageData.setId(id);
        tPdaPageData.setIsDel(true);
        tPdaPageData.setUpdateTime(LocalDateTime.now());
        tPdaPageData.setUpdateBy(userId);

        count = pageDataMapper.updateById(tPdaPageData);
        return count;
    }
}
